package com.aisilesn.base.controller;

import com.aisilesn.base.pojo.City;
import com.aisilesn.base.service.CityService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import util.IdWorker;

import java.util.List;
import java.util.Map;

/**
 * @Author: Mr.liu
 * @Description:
 * @Date: Create in 9:15 2019/4/24
 */
@RestController
@RequestMapping("/city")
@CrossOrigin
public class CityController {

    @Autowired
    CityService cityService;

    /**
     * 查询所有城市信息
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll(){
        List<City> list = cityService.findAll();
        return new Result(true, StatusCode.OK,"查询成功",list);
    }

    /**
     * 根据ID查询城市
     * @param cityId
     * @return
     */
    @RequestMapping(value = "/{cityId}",method = RequestMethod.GET)
    public Result findByID(@PathVariable(value = "cityId") String cityId){
        City city = cityService.findById(cityId);
        return new Result(true,StatusCode.OK,"查询成功",city);
    }

    /**
     * 新增城市
     * @param city
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(City city){
        cityService.add(city);
        return new Result(true,StatusCode.OK,"增加成功");
    }

    /**
     * 修改城市
     * @param cityId
     * @param city
     * @return
     */
    @RequestMapping(value = "/{cityId}",method = RequestMethod.PUT)
    public Result findById(@PathVariable(value = "cityId") String cityId,City city){
        city.setId(cityId);
        cityService.update(city);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * 删除城市
     * @param cityId
     * @return
     */
    @RequestMapping(value = "/{cityId}",method = RequestMethod.DELETE)
    public Result deleteById(@PathVariable(value = "cityId") String cityId){
        cityService.deleteById(cityId);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * 条件查询
     * @return
     */
    @RequestMapping(value = "/search",method = RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap){
        List<City> search = cityService.findSearch(searchMap);
        return new Result(true,StatusCode.OK,"查询成功",search);
    }

    /**
     * 分页条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/search/{page}/{size}",method = RequestMethod.POST)
    public Result findSearchPage(@RequestBody Map searchMap,@PathVariable(value = "page") int page,@PathVariable(value = "size") int size){
        Page<City> searchPage = cityService.findSearchPage(searchMap,page-1,size);
        PageResult<City> cityPageResult = new PageResult<>(searchPage.getTotalElements(), searchPage.getContent());
        return new Result(true,StatusCode.OK,"查询成功",cityPageResult);
    }
}
